/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.easoft.code.pos.integration.cae.util;

/**
 *
 * @author Jose
 */
public class Tokenizer {

    public Tokenizer(String str, String delimiter) {
        OFFSET = 0;
        this.str = str;
        DELIMITER = delimiter;
    }

    public String nextToken() {
        int end = str.indexOf(DELIMITER, OFFSET);
        String newString = str.substring(OFFSET, end >= 0 ? end : str.length());
        OFFSET += newString.length() + DELIMITER.length();
        return newString;
    }

    public boolean hasMoreTokens() {
        return OFFSET < str.length();
    }

    public int countTokens() {
        int tokens = 0;
        for (int OFFSET_TMP = 0; OFFSET_TMP < str.length();) {
            int end = str.indexOf(DELIMITER, OFFSET_TMP);
            String newString = str.substring(OFFSET_TMP, end >= 0 ? end : str.length());
            OFFSET_TMP += newString.length() + DELIMITER.length();
            tokens++;
        }

        return tokens;
    }

    public static void main(String args[]) {
        for (Tokenizer stk = new Tokenizer("1|2|3||5|6|", "|"); stk.hasMoreTokens(); System.out.println("> " + stk.nextToken()));
    }

    private String str;
    private String DELIMITER;
    private int OFFSET;
}
